/*
 * Title: 师兄帮帮忙（ Typical Homework）
 * Author: Yuanshun L
 * Created: 02-Otc-2021
 * Note: Only one codeing head read and one coding text read.
 */

#include<stdio.h>
#include<string.h>

struct student{
    char sid[11],cid[21],name[11];
    int scores[4];
} stu[101];

void print_welcome();
int check_sid(const char *s);
int add(int n);
void move_forward_one_step_away(int start,int end);
void remove(char*s,int *number,char type);
void find(char type, char *keyword);
void get_statistics();
void print_all();

// 当前存在的学生人数
int num=0;

int main() {
    int option;
    int n;
    char temp[21];
    int flag;

    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);

    print_welcome();
    while(scanf("%d",&option)==1 && option){
        flag=1;
        switch(option){
            case 1: // 增加
                printf("Please enter the SID, CID, name and four scores. Enter 0  to finish.\n");
                if(add(num)){
                    num++;
                }
                else{
                    printf("Duplicated SID.\n");
                }
                break;
            case 2: // 删除
                printf("Please enter SID or name. Enter 0 to finish.\n");
                scanf("%s",temp);
                n = 0;
                if(temp[0]>='A' && temp[0]<='Z')
                    remove(temp,&n,'N');
                else
                    remove(temp,&n,'S');
                printf("%d student(s) removed.\n",n);
                break;
            case 3: // 查询
                printf("Please enter SID or name. Enter 0 to finish.\n");
                scanf("%s",temp);
                if(temp[0]>='A' && temp[0]<='Z')
                    find('N',temp);
                else
                    find('S',temp);
                break;
            case 4: // 排名
                printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
                // 输出排名
                break;
            case 5: // 统计
                printf("Please enter class ID, 0 for the whole statistics.\n");
                // 输出统计信息
                get_statistics();
                break;
            default: // 放弃
                // 退出
                flag=0;
                printf("Quit.\n");
        }
        if(flag){
            printf("\n");
            print_welcome();
        }
    }
    return 0;
}

// 打印欢迎信息
void print_welcome(){
    printf("Welcome to Student Performance Management System (SPMS).\n");
    printf("1 - Add\n");
    printf("2 - Remove\n");
    printf("3 - Query\n");
    printf("4 - Show ranking\n");
    printf("5 - Show statistics\n");
    printf("0 - Exit\n");
}

// 检查学号是否存在，不存在则返回假
int check_sid(const char *s){
    int i;
    for(i=0;i<num;i++){
        if(strcmp(stu[i].sid,s)==0){
            return 1;
        }
    }
    return 0;
}

// 增加一个学生的信息，增加成功则返回真
int add(int n){
    char buf[11],buf2[21],name2[11];
    int sc[4];
    scanf("%s%s%s",buf,buf2,name2);
    for(int i=0;i<4;i++){
        scanf("%d",&sc[i]);
    }
    if(!check_sid(buf)){
        memcpy(stu[n].sid,buf,sizeof(buf));
        memcpy(stu[n].cid,buf2,sizeof(buf2));
        memcpy(stu[n].name,name2,sizeof(name2));
        memcpy(stu[n].scores,sc,sizeof(sc));
        return 1;
    }

    return 0;

}

// start之后整体往前移动一步
void move_forward_one_step_away(int start,int end){
    for(int i=start;i<end;i++){
        stu[i]=stu[i+1];
    }
}

// 根据sid或者name移除学生
void remove(char*s,int *number,char type){
    // 被删除学生的人数
    int count =0;

    // 根据名称查找
    if(type=='N'){
        for(int i=num-1;i>=0;i--){
            if(strcmp(stu[i].name,s)==0){
                move_forward_one_step_away(i,num-1);
                num--;
                count++;
            }
        }
    }
        // 根据学号查找
    else{
        // 只需要找一次
        for(int i=0;i<num;i++){
            if(strcmp(stu[i].sid,s)==0){
                move_forward_one_step_away(i,num-1);
                num--;
                count++;
                break;
            }
        }
    }
    *number = count;
}

// 列出某个sid或者name的学生的信息
void find(char type, char *keyword){
    if(type=='N'){
        for(int i=0;i<num;i++){
            if(strcmp(stu[i].name,keyword)==0){
                printf("%s %s %s",stu[i].sid,stu[i].cid,stu[i].name);
                for(int j=0;j<4;j++){
                    printf(" %d",stu[i].scores[j]);
                }
                printf("\n");
            }
        }
    }
    else{
        for(int i=0;i<num;i++){
            if(strcmp(stu[i].sid,keyword)==0){
                printf("%s %s %s",stu[i].sid,stu[i].cid,stu[i].name);
                for(int j=0;j<4;j++){
                    printf(" %d",stu[i].scores[j]);
                }
                printf("\n");
                break;
            }
        }
    }
}

// 统计各科平均分、通过人数、未通过人数，总体上统计通过1,2,3,4,0科的人数
void get_statistics(){
    // 把所有学生的各科成绩相加
    int total[4];

    // pass1表示通过index门数的学生数量
    // pass2表示通过语文、数学、英语和编程的人数
    int pass1[5],pass2[4];
    int pass_num =0;
    int score;

    memset(total,0,sizeof(total));
    memset(pass1,0,sizeof(pass1));
    memset(pass2,0,sizeof(pass2));

    for(int i=0;i<num;i++){
        pass_num = 0;
        for(int j=0;j<4;j++){
            score = stu[i].scores[j];
            total[j]+= score;
            if(score>=60){
                pass_num++;
                pass2[j]++;
            }
        }
        pass1[pass_num]++;
    }

    char subjects[][10] = {"Chinese","Math","English","Program"};
    printf("\n");
    for(int i=0;i<4;i++){
        printf("%s\n",subjects[i]);
        printf("Average Score: %.2f\n",1.0*total[i]/num);
        printf("Number of passed students: %d\n",pass2[i]);
        printf("Number of failed students: %d\n",num-pass2[i]);
    }
    printf("Overall:\n");
    printf("Number of students who passed all subjects: %d\n",pass1[4]);
    printf("Number of students who passed 3 or more subjects: %d\n",pass1[4]+pass1[3]);
    printf("Number of students who passed 2 or more subjects: %d\n",pass1[4]+pass1[3]+pass1[2]);
    printf("Number of students who passed 1 or more subjects: %d\n",pass1[4]+pass1[3]+pass1[2]+pass1[1]);;
    printf("Number of students who failed all subjects: %d\n",pass1[0]);
}

// 按照输入的格式打印所有学生的信息
void print_all(){
    for(int i=0;i<num;i++){
        printf("%s %s %s\n",stu[i].sid,stu[i].cid,stu[i].name);
    }
}